<?php

namespace app\common\service\order\coupon_order\jutuike_channel;

use app\common\service\order\coupon_order\base\SelfEmployedBase;
use app\common\service\order\coupon_order\base\SelfEmployedBaseV2;
use app\model\BaseSupplier;
use app\model\CouponOrder;
use think\Exception;

class CouponOrderJutuikeBuild extends SelfEmployedBaseV2
{
    /**
     * @var BaseSupplier|array|\think\Model
     */
    public $supplierInfo;

    /**
     * 注意 $this->couponOrder->params 参数说明
     * params['goodsInfoList'] 聚推客周边游商品列表的数据 单位是分  cheap_items 套餐价最低的套餐信息
     *
     * params['checkCreateOrderJtkRes'] 聚推客 效验订单返回价格等重要数据，注意价格单位为：分。 注意其中 saleAmount 是 元 销售金额
     * checkCreateOrderJtkRes 返回字段的几个价格说明 以下
     * channelPrice 结算价 saleAmount 套餐出售总金额 单位元 salePrice 套餐价 originPrice 原价 quantity 购买数量
     */

    public function __construct(CouponOrder $couponOrder) {

        $this->couponOrder = $couponOrder;
//        $agent_id = $this->couponOrder->agent_id ?? $this->agent_id;

    }

    function getServType()
    {
        return PRODUCT_AROUND_SHOP_SERV;
    }

    function getType()
    {
        return CouponOrder::TYPE_COUPON_SELF_EMPLOYED;
    }

    public function getSupplierId() {
        $jutuikeInfo = BaseSupplier::where('code', BaseSupplier::CODE_JUTUIKE)
            ->where('serv_type', $this->couponOrder->serv_type)
            ->find();

        if (!$jutuikeInfo) {
            throw new Exception('不存在聚推客配置信息');
        }
        if (empty($jutuikeInfo->api_config['apikey'])) {
            throw new Exception('缺少配置信息');
        }

        $this->supplierInfo = $jutuikeInfo;
        return $jutuikeInfo->id;
    }

    /**
     * 支付金额
     * @return float|mixed|string|void
     */
    public function getPayAmount() {
        // 聚推客返回的是 元
        $params = $this->couponOrder->params;
        // 聚推客saleAmount 字段感觉不对，还是根据套餐价*数量 得到支付金额
        if (isset($params['checkCreateOrderJtkRes']['salePrice'])) {
            $quantity =  $params['checkCreateOrderJtkRes']['quantity'];
            $originPrice = $params['checkCreateOrderJtkRes']['salePrice'];
            // 套餐成本价 * 购买数量
            $totalPrice = bcmul($originPrice, $quantity);
            return round($totalPrice);
        }
        if (isset($params['goodsInfoList']['cheap_items'])) {
            return $params['goodsInfoList']['cheap_items']['salePrice'];
        }
        return '';
    }

    // 成本价
    public function getOriginAmount() {
        $params = $this->couponOrder->params;
        if (isset($params['checkCreateOrderJtkRes']['originPrice'])) {
            $quantity =  $params['checkCreateOrderJtkRes']['quantity'];
            $originPrice = $params['checkCreateOrderJtkRes']['originPrice'];
            // 套餐成本价 * 购买数量
            $totalPrice = bcmul($originPrice, $quantity);
            return round($totalPrice);
        }
        if (isset($params['goodsInfoList']['cheap_items'])) {
            return $params['goodsInfoList']['cheap_items']['originPrice'];
        }
        return '';
    }

    // 供应商结算价  我们与商户的结算价，即为商户的成本价
    public function getSupplierPrice() {
        $params = $this->couponOrder->params;
        if (isset($params['checkCreateOrderJtkRes']['channelPrice'])) {
            $channelPrice = $params['checkCreateOrderJtkRes']['channelPrice'];
            $quantity =  $params['checkCreateOrderJtkRes']['quantity'];
            // 套餐的单价 * 数量
            $totalPrice = bcmul($channelPrice, $quantity);
            return round($totalPrice);
        }

        if (isset($params['goodsInfoList']['cheap_items'])) {
            return $params['goodsInfoList']['cheap_items']['channelPrice'];
        }

        return '';
    }

    /**
     * 总佣金  注意单位是元 底层会转成分
     * @return int
     */
    public function getOrderAllCommission()
    {
        // 用户支付的金额 - 平台付给供应商结算的钱
        $price = $this->pay_amount - $this->supplier_price;
        return fen2yuan($price);
    }




    public function getAmount()
    {
        return $this->pay_amount;
    }

    // 产品名称
    public function getProductName() {
        $params = $this->couponOrder->params;
        if (isset($params['jutuikeGoodsInfo'])) {
            return $params['jutuikeGoodsInfo']['productTitle'] ?? '';
        }
        return '';
    }

    // 产品图片
    public function getProductImage() {
        $params = $this->couponOrder->params;
        if (isset($params['jutuikeGoodsInfo'])) {
            return $params['jutuikeGoodsInfo']['faceImg'] ?? '';
        }
        return '';
    }

    // 账号
    public function getAccount() {
        return $this->couponOrder->params['customerPhoneNumber'] ?? '';
    }

    // 客户手机号
    public function getMobile() {
        return $this->couponOrder->params['customerPhoneNumber'] ?? '';
    }

    // 关联字段 商品表的Id
    public function getQuoteId() {
        $params = $this->couponOrder->params;
        if (isset($params['localJtkGoodsId'])) {
            return $params['localJtkGoodsId'];
        }
        return '';
    }

    public function getExtData()
    {
        return $this->couponOrder->params;
    }



}